﻿using System;
using System.Data;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using DevComponents.DotNetBar;
using PlanningGIS.Win.Controls;
using PlanningGIS.Util.IO;
using PlanningGIS.Util.Data;

namespace PlanningGIS.Report
{
    /// <summary>
    /// 作用：报表模版
    /// </summary>
    public partial class FrmReport : Office2007Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmReport()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 水晶报表的数据源【必需】
        /// </summary>
        public System.Data.DataTable DataSource
        {
            get;
            set;
        }
        
        /// <summary>
        /// 水晶报表的全路径名称【必需】
        /// </summary>
        public string FullName { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        {
            get { return this.Text; }
            set { this.Text = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public double ParameterValue
        {
            get;
            set;
        }

        /// <summary>
        /// 数据库链接实例
        /// </summary>
        public DbConnectionInfo Connect
        {
            get;
            set;
        }

        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StatisticReport_Load(object sender, EventArgs e)
        {
            try
            {
                RefreshReport();  
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox(ex.Message);
            }

        }

        /// <summary>
        /// 刷新报表
        /// </summary>
        public void RefreshReport()
        {
            if (DataSource == null || DataSource.Rows.Count == 0)
            {
                MsgHelper.ShowWarningMsgbox("报表数据源不存在或为空！");
                return;
            }
            if (string.IsNullOrEmpty(FullName) || string.IsNullOrWhiteSpace(FullName))
            {
                MsgHelper.ShowWarningMsgbox("报表文件的全路径名称为空！");
                return;
            }
            if (!System.IO.File.Exists(FullName))
            {
                MsgHelper.ShowWarningMsgbox("指定的报表不存在！" + FullName);
                return;
            }
            ReportDocument report = new ReportDocument();
            report.Load(FullName);
            report.SetDataSource(DataSource);

            if (Connect != null)
            {
                foreach (CrystalDecisions.Shared.IConnectionInfo connection in report.DataSourceConnections)
                {
                    connection.SetConnection(Connect.IntanceName, "", Connect.UserID, Connect.Password);
                }
            }
            if (ParameterValue > 0) report.SetParameterValue(0, ParameterValue);
            crystalReportViewer1.ReportSource = report;
        }


    }
}